#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

#define M   10001

int N = 0;
int tree[M] = {0};
int m = 0, n = 0;

void process()
{
    int i = 0, j = 0;
    short int* pm = (short int*)malloc(N*sizeof(short int));
    short int* pn = (short int*)malloc(N*sizeof(short int));
    short int  pml = 0;
    short int  pnl = 0;
    while(m != 0)
    {
        pm[pml++] = m;
        m = tree[m];
    }
    while(n != 0)
    {
        pn[pnl++] = n;
        n = tree[n];
    }

//    for(i = pml-1; i >= 0; i--)
//    {
//        printf("%d ", pm[i]);
//    }
//    printf("\n");
//    for(i = pnl-1; i >= 0; i--)
//    {
//        printf("%d ", pn[i]);
//    }
//    printf("\n");

    for(i = pml-1, j = pnl-1; i >= 0 && j >= 0; i--, j--)
    {
        if(pm[i] == pn[j])
        {
            continue;
        }
        else
        {
            printf("%d\n", pm[i+1]);
            return;
        }
    }
    if(i == -1)
    {
        printf("%d\n", pm[0]);
    }
    else
    {
        printf("%d\n", pn[0]);
    }

    free(pm);
    free(pn);
}

int main()
{
    //freopen("bin\\debug\\in.txt", "r", stdin);

    int T = 0;
    int i = 0, j = 0;
    int node = 0, pnode = 0;

    scanf("%d", &T);

    for(i = 0; i < T; i++)
    {
        memset(tree, 0, sizeof(tree[0])*M);
        scanf("%d", &N);
        for(j = 0; j < N-1; j++)
        {
            scanf("%d%d", &pnode, &node);
            tree[node] = pnode;
        }
        scanf("%d%d", &m, &n);

        process();
    }

    return 0;
}
